package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ShoppingCartsController {
    @Autowired
    ICartService iCartService;

    @GetMapping("shopping/carts")
    public ResponseData ShoppingCartsList(HttpServletRequest request) {

        String userInfo = (String) request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);

        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PostMapping ("/shopping/carts")
    public ResponseData ShoppingAddCarts(@RequestBody CartForm cartForm) {

        AddCartRequest request = new AddCartRequest();
        request.setUserId(cartForm.getUserId());
        request.setItemId(cartForm.getProductId());
        request.setNum(cartForm.getProductNum());
        AddCartResponse response = iCartService.addToCart(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(null);
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PutMapping("/shopping/carts")
    public ResponseData ShoppingPutCarts(@RequestBody CartForm cartForm) {
       UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setUserId(cartForm.getUserId());
        request.setItemId(cartForm.getProductId());
        request.setNum(cartForm.getProductNum());
        request.setChecked(cartForm.getChecked());
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData("成功");
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/shopping/carts/{uId}/{pId}")
    public ResponseData ShoppingDeleteCarts(@PathVariable("uId") Long uId,@PathVariable("pId") Long pId) {
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uId);
        request.setItemId(pId);

        DeleteCartItemResponse response = iCartService.deleteCartItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData("成功");
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData shoppingCartsDeleteSelective(@PathVariable("uid") Long uid){
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);
        DeleteCheckedItemResposne itemResposne = iCartService.deleteCheckedItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(itemResposne.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData("成功");
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(itemResposne.getMsg());
    }

    @PutMapping("/shopping/items")
    public ResponseData shoppingCartSelectAll(@RequestBody CheckAllItemRequest request){
        CheckAllItemResponse response = iCartService.checkAllCartItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData("成功");
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
